var capture = require("./mac-screen-capture");

util = {
    urlRE: /https?:\/\/([-\w\.]+)+(:\d+)?(\/([^\s]*(\?\S+)?)?)?/g,
    //  html sanitizer
    toStaticHTML: function (inputHtml) {
        inputHtml = inputHtml.toString();
        return inputHtml.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
    },
    //pads n with zeros on the left,
    //digits is minimum length of output
    //zeroPad(3, 5); returns "005"
    //zeroPad(2, 500); returns "500"
    zeroPad: function (digits, n) {
        n = n.toString();
        while (n.length < digits)
            n = '0' + n;
        return n;
    },
    //it is almost 8 o'clock PM here
    //timeString(new Date); returns "19:49"
    timeString: function (date) {
        var minutes = date.getMinutes().toString();
        var hours = date.getHours().toString();
        return this.zeroPad(2, hours) + ":" + this.zeroPad(2, minutes);
    },

    //does the argument only contain whitespace?
    isBlank: function (text) {
        var blank = /^\s*$/;
        return (text.match(blank) !== null);
    },

    setCookie:function (c_name,value,expires) {
        var exdate=new Date()
        if (expires) {
            exdate.setTime(exdate.getTime() + expires*1000);
            document.cookie=c_name+ "=" +escape(value)+
                ";expires="+exdate.toGMTString();
        } else {
            document.cookie=c_name+ "=" +escape(value);
        }
    },

    getCookie: function (c_name) {
        if (document.cookie.length>0) {
            c_start=document.cookie.indexOf(c_name + "=")
            if (c_start!=-1) {
                c_start=c_start + c_name.length+1
                c_end=document.cookie.indexOf(";",c_start)
                if (c_end==-1) c_end=document.cookie.length
                return unescape(document.cookie.substring(c_start,c_end))
            }
        }
        return ""
    },

    urlify: function(text) {
        var res = text.replace(/(http(s)?:\/\/.)?(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)/g, function(url) {
            if (!url.startsWith("http")) {
                return '<a href="' + "http://" + url + '">' + url + '</a>';
            } else {
                return '<a href="' + url + '">' + url + '</a>';
            }
        });
        return res;
    },
};


function isThisYear(d) {
  var d2 = new Date();
  return d.getFullYear() == d2.getFullYear();
}

function isSameDay(d1, d2) {
  return (d1.getFullYear() == d2.getFullYear() && d1.getMonth() && d2.getMonth() && d1.getDate() == d2.getDate());
}

function isToday(d) {
  var now = new Date();
  return isSameDay(d, now);
}
function isYestody(d1) {
  var d2 = new Date(Date.now() - 3600*24*1000);
  return isSameDay(d1, d2);
}

function isInWeek(d) {
  var t = Date.now() - 7*3600*24*1000;
  var d2 = new Date(t);
  return (d.getTime() > t && !isSameDay(d, d2));
}


var helper = {
    toTime: function (ts) {
        var date = new Date(ts);
        var y = date.getFullYear();
        var month = date.getMonth();
        var d = date.getDate();
        var H = date.getHours();
        var m = date.getMinutes();
        var day = date.getDay();

        var week = ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"];

        if (isToday(date)) {
          return H + ':' + (m < 10 ? '0' + m : m);
        } else if (isYestody(date)) {
          return "昨天 " + H + ':' + (m < 10 ? '0' + m : m);
        } else if (isInWeek(date)) {
          var s = week[day];
          return s + " " + H + ':' + (m < 10 ? '0' + m : m);
        } else if (isThisYear(date)) {
          return month + '-' + d + ' ' + H + ':' + (m < 10 ? '0' + m : m);
        } else {
          return y + "-" + month + '-' + d + ' ' + H + ':' + (m < 10 ? '0' + m : m);
        }
    },
};
